/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.business.service.impl;

import me.zhengjie.business.domain.Hljg;
import me.zhengjie.business.domain.Ymkdz;
import me.zhengjie.business.domain.sup.Gsgl;
import me.zhengjie.business.domain.sup.Rygl;
import me.zhengjie.business.repository.GsglRepository;
import me.zhengjie.business.repository.HljgRepository;
import me.zhengjie.business.repository.RyglRepository;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.exception.EntityExistException;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.utils.*;
import lombok.RequiredArgsConstructor;
import me.zhengjie.business.repository.YmkdzRepository;
import me.zhengjie.business.service.YmkdzService;
import me.zhengjie.business.service.dto.YmkdzDto;
import me.zhengjie.business.service.dto.YmkdzQueryCriteria;
import me.zhengjie.business.service.mapstruct.YmkdzMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author xiahongbo
* @date 2022-04-15
**/
@Service
@RequiredArgsConstructor
public class YmkdzServiceImpl implements YmkdzService {

    private final GsglRepository gsglRepository;
    private final YmkdzRepository ymkdzRepository;
    private final RyglRepository ryglRepository;
    private final HljgRepository hljgRepository;
    private final YmkdzMapper ymkdzMapper;

    @Override
    public Map<String,Object> queryAll(YmkdzQueryCriteria criteria, Pageable pageable){
        Page<Ymkdz> page = ymkdzRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(ymkdzMapper::toDto));
    }

    @Override
    public List queryFwxm(YmkdzQueryCriteria criteria) {
     /*   System.out.println(SecurityUtils.getCurrentUserId());
        Rygl rygl = ryglRepository.findByBisRyglUserId(SecurityUtils.getCurrentUserId());
        Hljg hljg = hljgRepository.findById(rygl.getGsgl().getBisGsglId()).orElseGet(Hljg::new);*/

        Gsgl gsgl = gsglRepository.queryGsgl(SecurityUtils.getCurrentUserId());
        Hljg hljg = hljgRepository.findById(gsgl.getBisGsglId()).orElseGet(Hljg::new);
        criteria.setHljg(hljg);
        String fwlx = hljg.getBisGsglCqhlfwlx();

        System.out.println(fwlx.indexOf("01"));
        List rsList = new ArrayList();
        if(fwlx.indexOf("01") != -1) {
            Map<String, Object> map = new HashMap<>();
            criteria.setBisYmkdzFwlx("94");
            List<YmkdzDto> list = ymkdzMapper.toDto(ymkdzRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
            map.put("fwxmList", list);
            map.put("fwlxcode", "01");
            map.put("fwlxname", "生活照料");
            rsList.add(map);
        }
        if(fwlx.indexOf("04") != -1) {
            Map<String, Object> map = new HashMap<>();
            criteria.setBisYmkdzFwlx("95");
            List<YmkdzDto> list = ymkdzMapper.toDto(ymkdzRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
            map.put("fwxmList", list);
            map.put("fwlxcode", "04");
            map.put("fwlxname", "医疗护理");
            rsList.add(map);
        }

        return rsList;
    }

    @Override
    public List<YmkdzDto> queryAll(YmkdzQueryCriteria criteria){
        return ymkdzMapper.toDto(ymkdzRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public YmkdzDto findById(String bisYmkdzId) {
        Ymkdz ymkdz = ymkdzRepository.findById(bisYmkdzId).orElseGet(Ymkdz::new);
        ValidationUtil.isNull(ymkdz.getBisYmkdzId(),"Ymkdz","bisYmkdzId",bisYmkdzId);
        return ymkdzMapper.toDto(ymkdz);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public YmkdzDto create(Ymkdz resources) {
        Gsgl gsgl = gsglRepository.queryGsgl(SecurityUtils.getCurrentUserId());
        Hljg hljg = hljgRepository.findById(gsgl.getBisGsglId()).orElseGet(Hljg::new);
        Ymkdz isExistYmkdz =  ymkdzRepository.queryBybisYmkdzYbbmAndHljg(resources.getBisYmkdzYbbm(), hljg);
        if(isExistYmkdz != null) {
            throw new BadRequestException("名称["+resources.getBisYmkdzYbmc()+"]已经存在");
        }

        resources.setBisYmkdzId(IdUtil.simpleUUID());
        resources.setHljg(hljg);
        return ymkdzMapper.toDto(ymkdzRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Ymkdz resources) {
        Ymkdz ymkdz = ymkdzRepository.findById(resources.getBisYmkdzId()).orElseGet(Ymkdz::new);
        ValidationUtil.isNull( ymkdz.getBisYmkdzId(),"Ymkdz","id",resources.getBisYmkdzId());
        ymkdz.copy(resources);
        ymkdzRepository.save(ymkdz);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String bisYmkdzId : ids) {
            ymkdzRepository.deleteById(bisYmkdzId);
        }
    }

    @Override
    public void download(List<YmkdzDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YmkdzDto ymkdz : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}